package com.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms.entity.User;
import com.wms.mapper.UserMapper;
import com.wms.security.EncryptionService;
import com.wms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p> * </p>
 *
 * @author 林鸿宇
 * @since 2024-06-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EncryptionService encryptionService;


    @Override
    public IPage<User> pageC(IPage<User> page) {
        // 查询所有用户，您可以根据需要在这里添加条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        return userMapper.selectPage(page, queryWrapper);
    }

    @Override
    public User findByNo(String no) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getNo, no);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public void register(User user) {
        // 对用户密码进行加密
        user.setPassword(encryptionService.encrypt(user.getPassword()));
        // 保存用户到数据库
        save(user);
    }
    @Override
    public String generateToken(User existingUser) {
        // 简单的 token 生成逻辑，实际应用中应该使用更安全的方法生成 token
        return "token_for_user_" + existingUser.getNo();
    }


}
